package com.ocom.ugcapi.controller;


import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.ocom.common.constant.WxServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.callpackage.*;
import com.ocom.common.request.payhandle.DishesInfoVo;
import com.ocom.common.request.payhandle.OrderAddPackagePayRequest;
import com.ocom.common.request.payhandle.OrderDishesPayRequest;
import com.ocom.common.request.ugcapi.OrderMealPlanSubmitRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.*;
import com.ocom.common.vo.pay.CheckVo;
import com.ocom.common.vo.ugcapi.GzhUserInfo;
import com.ocom.redis.util.RedisUtil;
import com.ocom.ugcapi.client.PayConfigClientService;
import com.ocom.ugcapi.client.PayHandleClientService;
import com.ocom.ugcapi.mq.product.ActiveQuiryProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class VideoPhoneController {

    @Autowired
    private PayConfigClientService payConfigClientService;

    @Autowired
    private PayHandleClientService payHandleClientService;

    @Autowired
    private ActiveQuiryProduct activeQuiryProduct;

    @Resource
    RedisUtil redisUtil;

    @PostMapping(WxServiceUrl.Url_queryWallect)
    public Result querySubAccount(@RequestBody Map map) {
        QuerySubAccountRequest request = JSON.parseObject(JSON.toJSONString(map), QuerySubAccountRequest.class);
        GzhUserInfo user = request.getUserInfo();
        request.setComId(user.getComId());
        request.setPId(user.getPId());
        return payConfigClientService.querySubAccount(request);
    }

    @PostMapping(WxServiceUrl.Url_queryPackagePay)
    public Result getPackagePay(@RequestBody Map map) {
        GetPackagePayRequest request = JSON.parseObject(JSON.toJSONString(map), GetPackagePayRequest.class);
        GzhUserInfo user = request.getUserInfo();
        request.setComId(user.getComId());
        request.setPId(user.getPId());
        return payConfigClientService.getPackagePay(request);
    }

    @PostMapping(WxServiceUrl.Url_queryPackagePayDetail)
    public Result getPackagePayDetail(@RequestBody Map map) {
        GetPackagePayDetailRequest request = JSON.parseObject(JSON.toJSONString(map), GetPackagePayDetailRequest.class);

        GzhUserInfo user = request.getUserInfo();
        request.setComId(user.getComId());
        request.setPId(user.getPId());
        return payConfigClientService.getPackagePayDetail(request);
    }

    @PostMapping(WxServiceUrl.Url_queryPackageConfig)
    public Result getCallPackageConfig(@RequestBody Map map) {
        OnShowRequest request = JSON.parseObject(JSON.toJSONString(map), OnShowRequest.class);
        GzhUserInfo user = request.getUserInfo();
        request.setComId(user.getComId());

        return payConfigClientService.getCallPackageConfig(request);
    }

    @PostMapping(WxServiceUrl.Url_PackagePay)
    public Result addPackagePay(@RequestBody Map map) {
        AddPackagePayRequest request = JSON.parseObject(JSON.toJSONString(map), AddPackagePayRequest.class);

        GzhUserInfo user = request.getUserInfo();
        request.setComId(user.getComId());
        request.setPId(user.getPId());

        OnShowRequest onShowRequest = new OnShowRequest();
        onShowRequest.setComId(user.getComId());
        onShowRequest.setSize(1);
        onShowRequest.setPackageTypeId(request.getPackageTypeId());

        Result result = payConfigClientService.getCallPackageConfig(onShowRequest);
        if (200 != result.getCode() && null != result.getData()) {
            return ResultUtil.error(ResultEnum.Err_1000);
        }

        List<OnShowResponse> onShowResponses = JSON.parseArray(JSON.toJSONString(result.getData()), OnShowResponse.class);
        if (0 == onShowResponses.size()) {
            return ResultUtil.error(ResultEnum.Err_1000);
        }
        OnShowResponse onShowResponse = onShowResponses.get(0);
        //		URL_getXfUserInfo
        OrderAddPackagePayRequest orderDishesPayRequest = new OrderAddPackagePayRequest();

        String ord = RandomUtil.generateNumber(8);
        Date date = new Date();
        String orderTime = DateUtils.getStrDate(date, DateUtils.YYYYMMDDHHMMSS);
        String orderNo2 = ord + orderTime;
        orderNo2 = "SP" + NumConvertUtil.IntToHexStringNum(request.getUserInfo().getComId().intValue(), 6) + orderNo2;

        orderDishesPayRequest.setComId(request.getUserInfo().getComId());
        orderDishesPayRequest.setMachNo(request.getUserInfo().getAppId());
        orderDishesPayRequest.setTradeNo(orderNo2);
        orderDishesPayRequest.setAmount(onShowResponse.getPrice());
        orderDishesPayRequest.setType(request.getType());
        orderDishesPayRequest.setConsType(100);//1饭堂 2商店 100视频通话
        orderDishesPayRequest.setOpenId(request.getUserInfo().getOpenId());
        orderDishesPayRequest.setPId(request.getUserInfo().getPId());
        orderDishesPayRequest.setConsState(1);
        orderDishesPayRequest.setPayWay(request.getPayWay());

        orderDishesPayRequest.setStartDate(request.getStartDate());
        orderDishesPayRequest.setPackageTypeId(request.getPackageTypeId());

        orderDishesPayRequest.setMerchId(onShowResponse.getMerchId());
//            orderDishesPayRequest.setPointId(request.getPointId());
//            orderDishesPayRequest.setReceiptId(request.getReceiptId());
//            orderDishesPayRequest.setReceiptName(request.getReceiptName());
//            orderDishesPayRequest.setMealDate(DateUtil.getDateTime(request.getStartDate()));
//            orderDishesPayRequest.setMealNo(request.getMealNo());
//            orderDishesPayRequest.setDishesInfo(request.getMealMenuInfo());

        //4. mq延时查询
        Gson gson = new Gson();
        CheckVo checkVo = new CheckVo();
        checkVo.setMachNo(request.getUserInfo().getAppId());
        checkVo.setCompanyId(request.getUserInfo().getComId());
        checkVo.setOutTradeNo(orderNo2);
        activeQuiryProduct.sendDelayMessage(gson.toJson(checkVo));

        String canKeyUser = "accessSendSms#comId:" + request.getUserInfo().getComId() + "#pId:" + request.getUserInfo().getPId();
        redisUtil.del(canKeyUser);

        return payHandleClientService.orderDishesPay3(orderDishesPayRequest);
    }

}
